package com.wlhse.controller;

import com.wlhse.service.UploadService;
import com.wlhse.util.*;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import com.wlhse.util.token.TokenUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

@RequestMapping("/api/v1")
@Controller("FileUploadUtils")
public class FileUploadUtils {

    @Resource
    private UploadService uploadService;

    public String setFile(MultipartFile file, String str) throws Exception {

        String rootPath = System.getProperty("catalina.home") + "\\webapps\\" + str;
        // 原始名称
        String originalFileName = file.getOriginalFilename();
        // 新文件名
        String token = TokenUtil.generateString();
        String newFileName = token + originalFileName.substring(originalFileName.lastIndexOf("."));
        File newFile = new File(rootPath + File.separator + File.separator + newFileName);
        if (!newFile.getParentFile().exists()) {
            newFile.getParentFile().mkdirs();
        }
        file.transferTo(newFile);
        return newFileName;
    }

    @RequestMapping(value = "/file_upload", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String uploadFile(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return NR.r(CodeDict.CODE_MESSAGE, -1, CodeDict.UPLOAD_EMPTY, null, null, 0, 0);
        } else {
            String fileName = setFile(file, "resources\\");
            return NR.r(CodeDict.CODE_MESSAGE_DATA, 0, 0, fileName, null, 0, 0);
        }
    }

    @RequestMapping(value = "/problem_excel_upload", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String uploadProblemExcel(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return NR.r(CodeDict.CODE_MESSAGE, -1, CodeDict.UPLOAD_EMPTY, null, null, 0, 0);
        } else {
            String fileName = setFile(file, "ProblemExcel\\");
            String path = System.getProperty("catalina.home") + "\\webapps\\ProblemExcel\\" + fileName;
            uploadService.uploadProblemExcel(path);
            return NR.r(CodeDict.CODE_MESSAGE, 1, 0, null, null, 0, 0);
        }
    }

    @RequestMapping(value = "/check_item_excel_upload", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String uploadCheckItemExcel(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return NR.r(CodeDict.CODE_MESSAGE, -1, CodeDict.UPLOAD_EMPTY, null, null, 0, 0);
        } else {
            String fileName = setFile(file, "CheckItemExcel\\");
            String path = System.getProperty("catalina.home") + "\\webapps\\CheckItemExcel\\" + fileName;
            uploadService.uploadCheckItemExcel(path);
            return NR.r(CodeDict.CODE_MESSAGE, 1, 0, null, null, 0, 0);
        }
    }

    @RequestMapping(value = "/employees_excel_upload", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String uploadEmployees(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return NR.r(CodeDict.CODE_MESSAGE, -1, CodeDict.UPLOAD_EMPTY, null, null, 0, 0);
        } else {
            String fileName = setFile(file, "Employees\\");
            String path = System.getProperty("catalina.home") + "\\webapps\\Employees\\" + fileName;
            return uploadService.uploadEmployees(path);
        }
    }

    @RequestMapping(value = "/problems_excel_upload", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String uploadProblems(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return NR.r(CodeDict.CODE_MESSAGE, -1, CodeDict.UPLOAD_EMPTY, null, null, 0, 0);
        } else {
            String fileName = setFile(file, "Problems\\");
            String path = System.getProperty("catalina.home") + "\\webapps\\Problems\\" + fileName;
            return uploadService.uploadProblems(path);
        }
    }

    @RequestMapping(value = "/description/excel/upload", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public R uploadProblemsDescription(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return R.error();
        } else {
            String fileName = setFile(file, "description\\");
            String path = System.getProperty("catalina.home") + "\\webapps\\Problems\\" + fileName;
            uploadService.uploadProblemDescription(path);
            return null;
        }
    }

    @RequestMapping(value = "/check_list_excel_upload", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String uploadCheckList(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return NR.r(CodeDict.CODE_MESSAGE, -1, CodeDict.UPLOAD_EMPTY, null, null, 0, 0);
        } else {
            String fileName = setFile(file, "CheckList\\");
            String path = System.getProperty("catalina.home") + "\\webapps\\CheckList\\" + fileName;
            return uploadService.uploadCheckList(path);
        }
    }


    @RequestMapping(value = "/employees_upload", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String uploadEmployees2(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return NR.r(CodeDict.CODE_MESSAGE, -1, CodeDict.UPLOAD_EMPTY, null, null, 0, 0);
        } else {
            String fileName = setFile(file, "Employees\\");
            String path = System.getProperty("catalina.home") + "\\webapps\\Employees\\" + fileName;
            return uploadService.uploadEmployees2(path);
        }
    }
    //melon-树形excel表格进数据库
    @RequestMapping(value = "/test_excel", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String test_excel(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return NR.r(CodeDict.CODE_MESSAGE, -1, CodeDict.UPLOAD_EMPTY, null, null, 0, 0);
        } else {
            String fileName = setFile(file, "CheckItemExcell\\");
            String path = System.getProperty("catalina.home") + "\\webapps\\CheckItemExcell\\" + fileName;
            uploadService.testExcel(path);
            return NR.r(CodeDict.CODE_MESSAGE, 1, 0, null, null, 0, 0);
        }
    }
    //melon-树形word进数据库
    @RequestMapping(value = "/test_word", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String test_word(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return NR.r(CodeDict.CODE_MESSAGE, -1, CodeDict.UPLOAD_EMPTY, null, null, 0, 0);
        } else {
            String fileName = setFile(file, "CheckItemWord\\");
            String path = System.getProperty("catalina.home") + "\\webapps\\CheckItemWord\\" + fileName;
            uploadService.testWord(path);
            return NR.r(CodeDict.CODE_MESSAGE, 1, 0, null, null, 0, 0);
        }
    }
}